package com.example.speeddating.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.annotaion.LoginUser;
import com.example.speeddating.annotaion.TokenCheck;
import com.example.speeddating.common.BaseResponse;
import com.example.speeddating.common.RespGenerator;
import com.example.speeddating.entity.Admin;
import com.example.speeddating.entity.Feedback;
import com.example.speeddating.entity.TokenEntity;
import com.example.speeddating.entity.User;
import com.example.speeddating.enums.ResponseCodeEnum;
import com.example.speeddating.exception.CommonException;
import com.example.speeddating.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:33
 */
@RestController
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    /**
     * 用户反馈
     * @param tokenEntity
     * @param jsonString
     * @return
     */
    @TokenCheck(roles = "user")
    @PostMapping("/user/feedback")
    public BaseResponse userFeedback(@LoginUser TokenEntity tokenEntity,@RequestBody HashMap<String,Object> jsonString){
        if (jsonString.get("content")==null||jsonString.get("image1")==null){
            throw new CommonException(ResponseCodeEnum.CODE_600);
        }
        Feedback feedback = new Feedback();
        feedback.setUserId(tokenEntity.getId());
        feedback.setContent(jsonString.get("content").toString());
        feedback.setImage1(jsonString.get("image1").toString());
        if (jsonString.get("image")!=null){
            feedback.setImage2(jsonString.get("image2").toString());
        }
        return RespGenerator.returnOK(feedbackService.save(feedback));
    }

    /**
     * 所有反馈信息
     * @param admin
     * @param pageNum
     * @param pageSize
     * @return
     */
    @TokenCheck(roles = "admin")
    @GetMapping("/admin/user/allFeedback")
    public BaseResponse<Page> allFeedback(@LoginUser Admin admin, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum , @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        Page<Feedback> page = feedbackService.feedBackList(pageNum, pageSize);
        return RespGenerator.returnOK(page);
    }

    /**
     * 所有反馈处理
     * @param admin
     * @param id
     * @return
     */
    @TokenCheck(roles = "admin")
    @PutMapping("/admin/feedback/handle/{id}")
    public BaseResponse<Page> feedbackHandle(@LoginUser Admin admin,@PathVariable Long id){
        if (id==null){
            throw new CommonException("请求参数错误");
        }
        feedbackService.handleFeedback(id);
        return RespGenerator.returnOK("修改成功");
    }
    /**
     *
     * 删除反馈信息
     * @param admin
     * @return
     */
    @TokenCheck(roles = "admin")
    @DeleteMapping("/admin/feedback/delete/{id}")
    public BaseResponse<Page> deleteFeedback(@LoginUser Admin admin,@PathVariable Long id){
        feedbackService.deleteFeedback(id);
        return RespGenerator.returnOK("删除成功");
    }

}
